const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    calendar_show: false,
    start_time:'',
    end_time:'',
    time_type : 'day',
    minDate: new Date(2024, 0, 1).getTime(),
    maxDate: new Date().getTime(),
    isLoading: true,
    dataType: -1,
    page: 1,
    no_more: false,

    sec_user_lists:[
      { text: '全部分销商', value: 0 },
    ],
    device_lists: [
      { text: '全部柜点', value: 0 },
    ],
    status_lists: [
      { text: '全部订单', value: '-1' },
      { text: '未结算', value: '0' },
      { text: '已结算', value: '1' },
    ],
    type_lists: [
      { text: '全部渠道', value: '-1' },
      { text: '商城订单', value: '10' },
      { text: '抖音订单', value: '20' },
      { text: '次卡订单', value: '200' },
      { text: '充值订单', value: '100' },
    ],
    device_value: 0,
    status_value: '-1',
    type_value: '-1',
    sec_user_id : 0,
    order_type:'-1'

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 获取当前小程序的页面栈
    let pages = getCurrentPages();
    // 数组中索引最大的页面--当前页面
    let currentPage = pages[pages.length - 2];
    const shop_id = currentPage.options.shop_id;
    const agent_user = currentPage.data.agentUser ? currentPage.data.agentUser : currentPage.data.agent
    // 获取设备列表
    this.getDevice(shop_id);
    // 获取全部分销商
    if(agent_user.user_type == 1){
      this.getSecuser();
    }
    this.setData({
      agent_user:agent_user,
      device_value: shop_id == undefined ? 0 : shop_id
    })
    // 获取订单列表
    this.getOrderList();
  },

  /**
   * 获取订单列表
   */
  getOrderList: function(isNextPage, page) {
    let _this = this;
    App._get('user.agent.order/lists', {
      shop_id:_this.data.device_value,
      settled: _this.data.dataType,
      sec_user_id:_this.data.sec_user_id,
      start_time: _this.data.start_time,
      end_time : _this.data.end_time,
      time_type : _this.data.time_type,
      order_type : _this.data.order_type,
      page: page || 1,
    }, function(result) {
      // 创建页面数据
      _this.setData(_this.createData(result.data, isNextPage));
    });
  },

  /**
   * 创建页面数据
   */
  createData: function(data, isNextPage) {
    data['isLoading'] = false;
    // 列表数据
    let dataList = this.data.list;
    if (isNextPage == true && (typeof dataList !== 'undefined')) {
      data.list.data = dataList.data.concat(data.list.data)
    }
    // 当前用户id
    data['user_id'] = App.getUserId();
    // 导航栏数据
    data['tabList'] = [{
      value: -1,
      text: '全部订单',
    }, {
      value: 0,
      text: '未结算',
    }, {
      value: 1,
      text: '已结算',
    }];
    
    return data;
  },

  /**
   * 设置swiper的高度
   */
  setSwiperHeight: function() {
    // 获取系统信息(拿到屏幕宽度)
    let systemInfo = wx.getSystemInfoSync(),
      rpx = systemInfo.windowWidth / 750, // 计算rpx
      tapHeight = Math.floor(rpx * 82), // tap高度
      swiperHeight = systemInfo.windowHeight - tapHeight; // swiper高度
    this.setData({
      swiperHeight
    });
  },

  /** 
   * 点击tab切换 
   */
  swichNav: function(e) {
    let _this = this;
    _this.setData({
      dataType: e.target.dataset.current,
      list: {},
      page: 1,
      no_more: false,
      isLoading: true,
    }, function() {
      // 获取订单列表
      _this.getOrderList();
    });
  },

  /**
   * 下拉到底加载数据
   */
  triggerDownLoad: function() {
    // 已经是最后一页
    if (this.data.page >= this.data.list.last_page) {
      this.setData({
        no_more: true
      });
      return false;
    }
    // 获取订单列表
    this.getOrderList(true, ++this.data.page);
  },
  getDevice(sec_user_id){
    let _this = this;
    App._get('user.agent.device/lists', {sec_user_id:sec_user_id}, function(result) {
      // 创建页面数据
      const lists = result.data.lists.data;
      const device_tmp_lists = [];
      lists.map((item) => {
        if(item.shop_id > 0){
          device_tmp_lists.push({ 
            text: '['+item.agent_user.real_name+']'+item.shop.shop_name, 
            value: item.shop.shop_id 
          })
        }
      })
      let device_lists = [
        { text: '全部柜点', value: 0 },
      ];
      device_lists = device_lists.concat(device_tmp_lists)
      _this.setData({
        device_lists : device_lists,
      });
    });
  },
  getSecuser(){
    let _this = this;
    App._get('user.agent.team/lists', {}, function(result) {
      // 创建页面数据
      const lists = result.data.list;
      const sec_user_tmp_lists = [];
      lists.map((item) => {
        sec_user_tmp_lists.push({ 
          text: item.real_name, 
          value: item.user_id,
        })
      })
      const sec_user_lists = _this.data.sec_user_lists.concat(sec_user_tmp_lists)
      _this.setData({
        sec_user_lists : sec_user_lists,
      });
    });
  },
  onChangeSecuser(e,isNextPage,page){
    const sec_user_id = e.detail;
    let _this = this;
    _this.setData({
      device_value: 0,
      sec_user_id:sec_user_id
    })
    _this.getDevice(sec_user_id)
    // 创建页面数据
    this.getOrderList();
  },
  onChangeDevice(e,isNextPage,page){
    const shop_id = e.detail;
    let _this = this;
    _this.setData({
      device_value:shop_id
    })
    // 加载订单数据
    this.getOrderList();
  },
  onChangeStatus(e,isNextPage,page){
    const status = e.detail;
    let _this = this;
    _this.setData({
      dataType:status
    })
    // 加载订单数据
    this.getOrderList();
  },
  onChangeType(e,isNextPage,page){
    const type = e.detail;
    console.log(type,'111111')
    this.setData({
      order_type : type
    })
    // 加载订单数据
    this.getOrderList();
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  onCalendarShow(){
    this.setData({
      calendar_show:true
    })
  },
  onCalendarClose() {
    this.setData({ calendar_show: false });
  },
  onCalendarConfirm(event) {
    const [start, end] = event.detail;
    this.setData({
      calendar_show: false,
      start_time: this.formatDate(start),
      end_time :  this.formatDate(end),
      time_type : 'custom',
    });
    // 加载订单数据
    this.getOrderList();
  },
  resetTime(){
    this.setData({
      start_time : '',
      end_time : ''
    })
    // 加载订单数据
    this.getOrderList();
  },
  setTab(e){
    let type = e.currentTarget.dataset.type
    this.setData({
      time_type:type,
    })
    this.getOrderList();
    // 打开对应的时间选择器
  },
  onPopupClose(){
    this.setData({
      showTimePopup:false
    })
  },

})